comment.admin = liwe.module ( 'comment.admin' );

this._dest_div = 'block_main';
comment.admin.modules_lst = [];
comment.admin.admin_man = null;

comment.admin.set_dest_div = function ( div_id )
{
	this._dest_div = div_id;
};
/* --- ADMIN MANAGER FUNCTIONS --- */
comment.admin._admin_man_init = function ( dest_div )
{
        if ( comment.admin.admin_man ) return;
        comment.admin.admin_man = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'list_action': "comment.ajax.list",
                                        'del_action':  "comment.admin.delete",
                                        'mod_menu_actions': [],
					'filter_form' : comment.admin._filter_module,
					'filter_form_name' : "COM-mods-avail"
                                } );

        comment.admin.admin_man.set_row_fields (
                                [
                                        [ "id", "id" ],
					[ "ID rel.", "id_obj" ],
                                        [ "module", "module" ],
					[ "txt", "txt" ],
					[ "created", "created" ],
                                        [ "login", "login" ]
                                ]
                                );

        // comment.admin.admin_man.templates ['table_start' ] = comment.admin.templates [ 'table_start' ];
        // comment.admin.admin_man.templates [ 'table_row' ] = comment.admin.templates [ 'table_row' ];

        comment.admin.admin_man.cbacks [ 'row_manip' ] = comment.admin._row_manip;
        comment.admin.admin_man.cbacks [ 'del_item' ]  = function ( id ) { comment.admin.del ( id ); };

};

comment.admin._row_manip = function ( ds, row )
{
	row [ "_id" ] = row [ 'id' ];
	console.debug ( row );
};

comment.admin.list = function ( dest_div, module )
{
	/*
	if ( ! module ) 
	{
		if ( comment.admin.modules_lst.lenght < 1 ) return;
		module = comment.admin.modules_lst [ 0 ] [ 'module' ];
	}
	*/
	comment.admin.admin_man.show_list ( dest_div );
};

comment.admin.del = function ( id )
{
	var conf = false;
	conf = confirm ( "Vuoi eliminare il commento ?" );
	if ( conf )
	{
		comment.admin.ajax ( { action : "comment.admin.delete", id_comment : id }, function ( data )
					{
						comment.admin._filter_apply ();
					} );
	}
};

comment.admin._filter_module = function ( dest_div )
{
	if ( comment.admin.modules_lst.lenght < 1 ) return;
	var opts = [];
	var order_opts = [
				{ label: "data", value: "create" },
				{ label: "ID relativo", value: "page" },
				{ label: "ID utente", value: "uid" }
			 ];
	comment.admin.modules_lst.iterate ( function ( v ) 
				{
				 	opts.push ( { label : v [ 'module' ], value : v [ 'module' ] } );
				} );
	var f = new liwe.form.instance ( 'COM-mods-avail' );
	f.select (  { name : 'module', label : 'Moduli', options: opts, force_select: true, nonl: true } );
	f.select (  { name : 'order_by', label : 'Ordina per', options: order_opts } );
	f.button ( { name: "cerca", value: "cerca" , onclick: "comment.admin._filter_apply ();" } );
	f.set ( dest_div );	
};

comment.admin._filter_apply = function ()
{
	comment.admin.admin_man.search_start ();
};
 
comment.admin.init = function ()
{
	comment.admin.ajax ( { action : 'comment.admin.get_modules', id_site : site._id_site } , function ( data ) 
			{
				if ( data [ 'modules' ] ) comment.admin.modules_lst = data [ 'modules' ]; 
				var _acts = [
						{ label: 'Lista commenti', func: function () { comment.admin.list ( 'system-content' ); } }
					    ];
				comment.admin.register ( _acts );
				comment.admin._admin_man_init ( 'system-content' );	
			} );
};
